package com.steven.concurrency.example.atomic;


import com.steven.concurrency.annoations.ThreadSafe;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

@ThreadSafe
public class AtomicReferenceTest2 {

    private static AtomicIntegerFieldUpdater<AtomicReferenceTest2> atomicReference =
            AtomicIntegerFieldUpdater.newUpdater(AtomicReferenceTest2.class,"count");

    public volatile  int count=0;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public static void main(String[] args) {
        AtomicReferenceTest2 atomicReferenceTest2 = new AtomicReferenceTest2();

        if(atomicReference.compareAndSet(atomicReferenceTest2,0,100)){
            System.out.println(atomicReferenceTest2.getCount());
        }
        if(atomicReference.compareAndSet(atomicReferenceTest2,100,101)){
            System.out.println(atomicReferenceTest2.getCount());
        }

    }

}
